<?php


namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Goods extends Base
{
//商家管理首页
    public function index()
    {
        if (request()->isAjax()) {
            $status[0] = '下架';
            $status[1] = '上架';
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
             $gd = Db::name('goods');
             $res=$gd->where('shenhe','=',1)->page($page, $limit)->order('id','desc')->select();
//            $res = Db::name('goods')
//                ->alias('a')
//                ->join('goods_pinpai b', 'a.pinpai_id = b.id')
//                ->field('*, b.id as pid,a.id as gid')
//                ->page($page, $limit)->select();
           $pp=Db::name('goods_pinpai');
           $sj=Db::name('user_biz');
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['status2'] = $status[$re['status']];
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                if( !$re['thumb']){
                    $re['thumb']='null';
                    $res[$index]['thumb']=$re['thumb'];
                }else{
                    $res[$index]['thumb'] = $qiniu_config['domain'] . '/' . $re['thumb'] . $q_thumb;
                }
                $pinpai_data=$pp->where('id',$re['pinpai_id'])->field('pinpai')->find();
                if(!$pinpai_data){
                    $pinpai_data=['pinpai'=>'无'];
                }
                $sj_data=$sj->where('uid',$re['shop_uid'])->field('shop_name')->find();
                if(!$sj_data){
                    $sj_data=['shop_name'=>'无'];
                }
                $res[$index]['pinpai']=$pinpai_data;
                $res[$index]['shangjia']=$sj_data;
            }
            return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);

        } else {
            $count = Db::name('goods')->where('shenhe','=',1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

        // for ($i=0;$i<7;$i++){
        //     foreach ($res as $index => $re) {
        //         $data=$re;
        //         unset($data['id']);
        //         // var_dump($data);
        //         $gd->insertGetId($data);
        //     }
        // }
    }

    //设置商品状态（上下架）
    public function set_status()
    {
        $data = input('');
        $ids = $data['ids'];
        $status = $data['status'];

        if (!$ids) {
            $this->error('异常');
        }

        // $ids_str=implode(',',$ids);
        Db::name('goods')->where('id', 'in', $ids)->update(['status' => $status]);
        // $sql=Db::name('user')->getLastSql();
        // dump($sql);
        $this->success('操作成功');


    }
////查看商品简介及修改
//    public function look()
//    {
//        if (request()->isAjax()) {
//            $data = input('');
//            unset($data['file']);
//            $res = Db::name('goods')->where('id', $data['id'])->update($data);
//            if ($res === false) {
//                $this->success('操作失败');
//            }
//            $this->success('操作成功');
//
//
//        } else {
//            $id = input('id');
//            if (!$id) {
//                $this->error('id 异常');
//            }
//            $res = Db::name('goods')->find($id);
//            $this->assign('item', $res);
//            return $this->fetch();
//        }
//    }
//添加商品
    public function add()
    {
        if (request()->isAjax()) {
            $data = input('');
            $data['create_time'] = time();
            $data['shenhe'] = 1;
            unset($data['file']);
            unset($data['wz']);
            $res = Db::name('goods')->insertGetId($data);
            if ($res) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        } else {
            return $this->fetch();
        }

    }
    //添加商品中的店铺选择
     public function selectdp(){
         if (request()->isAjax()) {
             $page = input('page') ? input('page') : 1;
             $limit = input('limit') ? input('limit') : 10;
             $gd = Db::name('user_biz');

             $res = $gd->page($page, $limit)->select();
             $this->success('ok', '', $res);

         } else {
             $count = Db::name('user_biz')->count();
             $this->assign('count', $count);
             return $this->fetch();
         }
     }
    //添加商品中的店铺添加
     public function newdp(){
         if (request()->isAjax()) {
             $data = $data2 = input('');
             if (!$data['phone']) {
                 $this->error('请输入手机号');
             }
             // $data['phone']=rand(100000,9999999);
             $ret = Db::name('user')->where('phone', $data['phone'])->find();
             if ($ret) {
                 $this->error('手机号已存在');
             }

             if (!$data['password']) {
                 $this->error('请输入密码');
             }
             if (!$data['repassword']) {
                 $this->error('请输入密码');
             }
             if (!$data['phone']) {
                 $this->error('请输入联系方式');
             }
             if ($data['password'] != $data['repassword']) {
                 $this->error('两次密码不一致');
             }


             $data['is_biz'] = 1;

             unset($data['biz']);
             // unset($data['identity']);
             unset($data['repassword']);
             unset($data['file']);
             $data['password'] = cmf_password($data['password']);
             // 开启事务
             Db::startTrans();

             $uid = Db::name('user')->insertGetId($data);


             $flag = true;
             $worker = $data2['biz'];
             $worker['uid'] = $uid;

             if (!$worker['address']) {
                 $msg = '请输入地址';
                 $flag = false;

             }

             if (!$worker['sheng']) {

                 $msg = '请输入省份';
                 $flag = false;
             }

             if (!$worker['shi']) {

                 $msg = '请输入市';
                 $flag = false;
             }

             if (!$worker['xian']) {

                 $msg = '请输入区、县';
                 $flag = false;

             }

             if (!$worker['shop_name']) {

                 $msg = '请输入店名';
                 $flag = false;

             }

             if (!$flag) {
                 Db::rollback();
                 $this->error($msg);
             }

             $res=Db::name('user_biz')->insertGetId($worker);
             $worker['uid']=$res;
             if($res){
                 Db::commit();
                 $this->success('添加成功','',$worker);
             }

         } else {
             return $this->fetch();
         }
     }
     //添加商品中的品牌选择
    public function selectpinpai()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('goods_pinpai');

            $res = $gd->page($page, $limit)->select();
            $this->success('ok', '', $res);
        } else {
            $count = Db::name('goods_pinpai')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
        $data = input('');
        $id = $data['id'];
        $pinpai = $data['pinpai'];
        $res = Db::name('goods_pinpai')->where('id', $id)->where('pinpai', $pinpai)->find();
        if ($res) {
            $this->success('操作成功');
        }
    }
    //添加商品中的品牌添加
      public function newpinpai(){
          if (request()->isAjax()) {
              $data = input('');
              $data['create_time'] = time();
              unset($data['file']);
              $res = Db::name('goods_pinpai')->insertGetId($data);
              $data['id']=$res;
              if ($res) {
                  $this->success('操作成功','',$data);
              }
              $this->error('操作失败');
          } else {
              return $this->fetch();
          }
      }
    //商品管理界面详情
    public function detalis(Request $request)
    {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            $res = Db::name('goods')->find($id);
            if(!$res['thumb']){
                $res['thumb']="null";
                $res['thumb2']=$res['thumb'];
            }
            else{
                $res['thumb2'] = $qiniu_config['domain'] . '/' . $res['thumb'].$q_thumb;
            }
            $pp=Db::name('goods_pinpai');
            $sj=Db::name('user_biz');
            $pinpai_data=$pp->where('id',$res['pinpai_id'])->field('pinpai')->find();
            if(!$pinpai_data){
                $pinpai_data=['pinpai'=>'无'];
            }
            $sj_data=$sj->where('uid',$res['shop_uid'])->field('shop_name')->find();
            if(!$sj_data){
                $sj_data=['shop_name'=>'无'];
            }
            $res['pinpai']=$pinpai_data;
            $res['shangjia']=$sj_data;
            $this->assign('item', $res);
            return $this->fetch();
    }
      //商品管理界面编辑
    public function edit(Request $request)
    {
        if (request()->isAjax()) {
            $data = input('');
            unset($data['file']);
            unset($data['wz']);
            $res = Db::name('goods')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');


        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            $res = Db::name('goods')->find($id);
            if(!$res['thumb']){
                $res['thumb']="null";
                $res['thumb2']=$res['thumb'];
            }
            else{
                $res['thumb2'] = $qiniu_config['domain'] . '/' . $res['thumb'].$q_thumb;
            }
            $pp=Db::name('goods_pinpai');
            $sj=Db::name('user_biz');
                $pinpai_data=$pp->where('id',$res['pinpai_id'])->field('pinpai')->find();
                if(!$pinpai_data){
                    $pinpai_data=['pinpai'=>'无'];
                }
                $sj_data=$sj->where('uid',$res['shop_uid'])->field('shop_name')->find();
                if(!$sj_data){
                    $sj_data=['shop_name'=>'无'];
                }
                $res['pinpai']=$pinpai_data;
                $res['shangjia']=$sj_data;

//            $qiniu_config = cmf_get_option('qiniu');
//            // 七牛样式处理
//            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/200x200/quality/100/interlace/0";
//            $res['thumb2'] = $qiniu_config['domain'] . '/' . $res['thumb'] . $q_thumb;
            $this->assign('item', $res);
            return $this->fetch();
        }
    }

//商品管理界面商品的删除
    public function del()
    {
        $data = input('');
        $ids = $data['ids'];
        foreach ($ids as $id) {
            $res = Db::name('goods')->where('id', $id)->delete();
        }
        if ($res) {
            $this->success('操作成功');
        }
    }
//品牌管理界面
    public function pinpai()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $gd = Db::name('goods_pinpai');

            $res = $gd->page($page, $limit)->order('id','desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
                if( !$re['pinpai_logo']){
                    $re['pinpai_logo']='null';
                    $res[$index]['pinpai_logo']=$re['pinpai_logo'];
                }else{
                    $res[$index]['pinpai_logo'] = $qiniu_config['domain'] . '/' . $re['pinpai_logo'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('goods_pinpai')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }
//品牌管理界面的品牌添加
    public function addpinpai()
    {
        if (request()->isAjax()) {
            $data = input('');

            $data['create_time'] = time();
            unset($data['file']);
            $res = Db::name('goods_pinpai')->insertGetId($data);
            if ($res) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        } else {
            return $this->fetch();
        }

    }
//品牌管理界面的品牌编辑
    public function editpinpai()
    {
        if (request()->isAjax()) {
            $data = input('');
            unset($data['file']);
            $res = Db::name('goods_pinpai')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');


        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('goods_pinpai')->find($id);
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb="?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
            if(!$res['pinpai_logo']){
                $res['pinpai_logo']="null";
                $res['pinpai_logo2']=$res['pinpai_logo'];
            }
            else{
                $res['pinpai_logo2'] = $qiniu_config['domain'] . '/' . $res['pinpai_logo'].$q_thumb;
            }
            $this->assign('item', $res);
            return $this->fetch();
        }
    }
    //品牌管理界面的品牌删除
    public function delpinpai(){
        $data=input('');
        $ids = $data['ids'];
        foreach ($ids as $id){
            $res=Db::name('goods_pinpai')->where('id',$id)->delete();
        }
        if($res){
            $this->success('操作成功');
        }
    }
//    商品审核
  public function shenhe(){
    if (request()->isAjax()) {
        $shenhe[0] = '待审核';
        $shenhe[1] = '通过审核';
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $gd = Db::name('goods');
        $res=$gd->where('shenhe','=',0)->page($page, $limit)->order('id','desc')->select();
        $pp=Db::name('goods_pinpai');
        $sj=Db::name('user_biz');
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/quality/100/interlace/0";
        foreach ($res as $index => $re) {
            $res[$index]['shenhe2'] = $shenhe[$re['shenhe']];
            $res[$index]['create_time'] = date('Y-m-d H:i:s', $re['create_time']);
            if( !$re['thumb']){
                $re['thumb']='null';
                $res[$index]['thumb']=$re['thumb'];
            }else{
                $res[$index]['thumb'] = $qiniu_config['domain'] . '/' . $re['thumb'] . $q_thumb;
            }
            $pinpai_data=$pp->where('id',$re['pinpai_id'])->field('pinpai')->find();
            if(!$pinpai_data){
                $pinpai_data=['pinpai'=>'无'];
            }
            $sj_data=$sj->where('uid',$re['shop_uid'])->field('shop_name')->find();
            if(!$sj_data){
                $sj_data=['shop_name'=>'无'];
            }
            $res[$index]['pinpai']=$pinpai_data;
            $res[$index]['shangjia']=$sj_data;
        }
        return json(['code' => 1, 'msg' => 'ok', 'count' => count($res), 'data' => $res]);

    } else {
        $count = Db::name('goods')->where('shenhe','=',0)->count();
        $this->assign('count', $count);
        return $this->fetch('shenhe');
    }
  }
    //设置商品审核状态
    public function set_shenhe()
    {
        $data = input('');
        $ids = $data['ids'];
        $shenhe = $data['shenhe'];

        if (!$ids) {
            $this->error('异常');
        }

        // $ids_str=implode(',',$ids);
        Db::name('goods')->where('id', 'in', $ids)->update(['shenhe' => $shenhe]);
        // $sql=Db::name('user')->getLastSql();
        // dump($sql);
        $this->success('操作成功');


    }

}
